package com.kim.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.util.Map;

import com.kim.net.proxy.NetProxyInfo;
import com.kim.net.proxy.NetProxyInfoProxy;

public class NetUtil {

	/**
	 * 网络连接超时时间
	 */
	public static final int DEFAULT_CONNECT_TIMEOUT = 6000;

	/**
	 * 输入流读取时间
	 */
	public static final int DEFAULT_READ_TIMEOUT = 6000;

	/**
	 * 获取URL连接
	 * 
	 * @param urlStr
	 *            网络地址
	 * @param httpHeaders
	 *            请求头
	 * @param outputData
	 *            发送数据
	 * @return
	 * @throws IOException
	 */
	public static final HttpURLConnection getHttpURLConnection(String urlStr,
			Map<String, String> httpHeaders, String outputData)
			throws IOException {
		URL url = new URL(urlStr);
		NetProxyInfo proxyInfo = NetProxyInfoProxy.getInstance()
				.getNetProxyInfo();
		HttpURLConnection httpconn;
		if (proxyInfo == null || null != proxyInfo.host) {
			httpconn = (HttpURLConnection) url.openConnection();
			System.out
					.println("httpconn = (HttpURLConnection) url.openConnection();");
		} else {
			Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
					proxyInfo.host, proxyInfo.port));
			httpconn = (HttpURLConnection) url.openConnection(proxy);
			System.out
					.println("Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(");
		}

		// 设置请求头信息
		if (httpHeaders != null) {
			for (String key : httpHeaders.keySet()) {
				httpconn.addRequestProperty(key, httpHeaders.get(key));
			}
		}

		// 兼容GZIP压缩
		httpconn.addRequestProperty("Accept-Encoding", "gzip,deflate");

		httpconn.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT);
		httpconn.setReadTimeout(DEFAULT_READ_TIMEOUT);
		httpconn.setRequestMethod("POST");
		httpconn.setDoOutput(true);
		OutputStream outPs = null;
		outPs = httpconn.getOutputStream();
		byte[] datas = outputData.getBytes("UTF-8");
		outPs.write(datas, 0, datas.length);
		outPs.flush();

		return httpconn;
	}

	/**
	 * 获取URL连接
	 * 
	 * @param urlStr
	 *            网络地址
	 * @return
	 * @throws IOException
	 */
	public static final HttpURLConnection getHttpURLConnection(String urlStr)
			throws IOException {
		URL url = new URL(urlStr);
		NetProxyInfo proxyInfo = NetProxyInfoProxy.getInstance()
				.getNetProxyInfo();
		HttpURLConnection httpconn;
		if (proxyInfo == null || null != proxyInfo.host) {
			httpconn = (HttpURLConnection) url.openConnection();
			System.out
					.println("httpconn = (HttpURLConnection) url.openConnection();");
		} else {
			Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
					proxyInfo.host, proxyInfo.port));
			httpconn = (HttpURLConnection) url.openConnection(proxy);
			System.out
					.println("Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(");
		}
		return httpconn;
	}

	/**
	 * 将输入流转换为字符串
	 * 
	 * @param inputStream
	 * @return {@link String}
	 * @throws IOException
	 */
	public static final String convertInputStream2String(InputStream inputStream)
			throws IOException {
		if (inputStream == null || inputStream.available() == 0) {
			return null;
		}
		int len = -1;
		byte[] buf = new byte[1024];
		StringBuilder builder = new StringBuilder(inputStream.available());
		while ((len = inputStream.read(buf)) != -1) {
			builder.append(new String(buf, 0, len));
		}
		return builder.toString();
	}

	/**
	 * 将输入流转换为字符串
	 * 
	 * @param inputStream
	 * @return {@link String}
	 * @throws IOException
	 */
	public static final String convertInputStream2String(
			InputStream inputStream, String encoding) throws IOException {
		if (inputStream == null || inputStream.available() == 0) {
			return null;
		}
		int len = -1;
		byte[] buf = new byte[1024];
		StringBuilder builder = new StringBuilder(inputStream.available());
		while ((len = inputStream.read(buf)) != -1) {
			builder.append(new String(buf, 0, len, encoding));
		}
		return builder.toString();
	}

}
